package com.open.capacity.salary.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.salary.dao.GzBankAutoDao;
import com.open.capacity.salary.dao.GzReportDao;
import com.open.capacity.salary.service.GzBankAutoService;
import com.open.capacity.salary.service.GzEnableMonthService;
import com.open.capacity.salary.service.GzItemService;
import com.open.capacity.salary.service.GzReportService;
import com.open.capacity.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GzBankAutoServiceImpl implements GzBankAutoService {

    @Autowired
    private GzBankAutoDao gzReportDao;

    @Autowired
    private GzItemService gzItemService;

    @Autowired
    private GzEnableMonthService gzEnableMonthService;


    /**
     * 查询
     *
     * @param params
     * @return
     */
    public PageResult<Map<String, Object>> pageAll(Map<String, Object> params) {
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();

        //账套当前月份
        String currentMonth = gzEnableMonthService.findByZtId(companyId, ztId.split(",")[0]).getCurrentMonth();
        params.put("month", currentMonth);


        List<String> itemList = gzItemService.findByZtIds(companyId, ztId);
        params.put("itemColumns", itemList);
        params.put("ztId", Arrays.asList(ztId.split(",")));
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        String sidx = params.get("sidx").toString();
        String sord = params.get("sord").toString();
        if (StringUtil.isNotEmpty(sidx) && StringUtil.isNotEmpty(sord)) {
            params.put("orderby", sidx + " " + sord);
        }
        if (params.containsKey("departmentId") && StringUtil.isNotEmpty(params.get("departmentId").toString())) {
            String departmentId = params.get("departmentId").toString();
            params.put("departmentId", Arrays.asList(departmentId.split(",")));
        } else {
            params.remove("departmentId");
        }
        if (params.containsKey("userCategory") && StringUtil.isNotEmpty(params.get("userCategory").toString())) {
            String userCategory = params.get("userCategory").toString();
            params.put("userCategory", Arrays.asList(userCategory.split(",")));
        } else {
            params.remove("userCategory");
        }
        List<Map<String, Object>> list = gzReportDao.findAll(params);
        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                boolean existsItem = false;
                for (Map.Entry<String, Object> entry : list.get(i).entrySet()) {
                    if (entry.getKey().equals("money")) {
                        existsItem = true;
                        if (entry.getValue() == null || new BigDecimal(entry.getValue().toString()).compareTo(BigDecimal.ZERO) == 0) {
                            list.remove(i);
                            i--;
                            break;
                        }
                    }
                }
                if(!existsItem) {
                    list.remove(i);
                    i--;
                }
            }
        }
        PageInfo<Map<String, Object>> pageInfo = new PageInfo(list);

        return PageResult.<Map<String, Object>>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 工资查询
     *
     * @param params
     * @return
     */
    public List<Map<String, Object>> findAll(Map<String, Object> params) {
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        List<String> itemList = gzItemService.findByZtIds(companyId, ztId);
        params.put("itemColumns", itemList);
        params.put("ztId", Arrays.asList(ztId.split(",")));
        if (params.containsKey("departmentId") && StringUtil.isNotEmpty(params.get("departmentId").toString())) {
            String departmentId = params.get("departmentId").toString();
            params.put("departmentId", Arrays.asList(departmentId.split(",")));
        } else {
            params.remove("departmentId");
        }
        if (params.containsKey("userCategory") && StringUtil.isNotEmpty(params.get("userCategory").toString())) {
            String userCategory = params.get("userCategory").toString();
            params.put("userCategory", Arrays.asList(userCategory.split(",")));
        } else {
            params.remove("userCategory");
        }

        //机构当前月份
        String currentMonth = gzEnableMonthService.findByZtId(companyId, ztId.split(",")[0]).getCurrentMonth();
        params.put("month", currentMonth);
        return gzReportDao.findAll(params);
    }
}
